<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_DataGrid
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */




/**
 * Zend_DataGrid_Renderer
 *
 * @category   Zend
 * @package    Zend_DataGrid
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Renderer.php 8862 2008-09-11 15:36:00Z azoungrana $
 */
abstract class Zend_DataGrid_Renderer
{

    /**
     * The default renderer type
     * @var string
     */
    protected static $defaultRendererType = 'Html';

    /**
     * The grid component
     *
     * @var Zend_DataGrid|Zend_DataGrid_AssocColumns|Zend_DataGrid_Column
     */
    protected $_gridElement = null;

    /*
     * The options to set
     *
     * @var array
     */
    protected $_options = array();

    /**
     * @var Zend_View_Interface
     */
    protected $_view;


    /**
     * Constructor
     *
     * @param array|Zend_Config $options
     * @return void
     */
    public function __construct($options = null)
    {
        if ($options instanceof Zend_Config)
            $options = $options->toArray();

        $this->initOptions();

        if (null != $options)
            $this->setOptions($options);
    }

    /**
     * Factory
     *
     * @param string $rendererType
     * @param Zend_DataGrid|Zend_DataGrid_AssocColumns|Zend_DataGrid_Column $gridElement
     * @param array|Zend_Config $options
     * @return Zend_DataGrid_Renderer
     */
    public static function factory($rendererType, $gridElement, $options = array())
    {
        //construct the renderer class
        $rendererClass = $basicRendererClass = 'Zend_DataGrid_Renderer_' . $rendererType;

        if ($gridElement instanceof Zend_DataGrid) {
            $rendererClass .= '_Table';
        } else if ($gridElement instanceof Zend_DataGrid_AssocColumns) {
            $rendererClass .= '_AssocColumns';
        } else if ($gridElement instanceof Zend_DataGrid_Column_Button) {
            $rendererClass .= '_Button';
        } else if ($gridElement instanceof Zend_DataGrid_Column_Checkbox) {
            $rendererClass .= '_Checkbox';
        } else if ($gridElement instanceof Zend_DataGrid_Column_EditCommand) {
            $rendererClass .= '_EditCommand';
        } else if ($gridElement instanceof Zend_DataGrid_Column_HyperLink) {
            $rendererClass .= '_HyperLink';
        } else if ($gridElement instanceof Zend_DataGrid_Column_Image) {
            $rendererClass .= '_Image';
        } else if ($gridElement instanceof Zend_DataGrid_Column_Literal) {
            $rendererClass .= '_Literal';
        } else if ($gridElement instanceof Zend_DataGrid_Column_Radiobox) {
            $rendererClass .= '_Radiobox';
        } else if ($gridElement instanceof Zend_DataGrid_Column_Selectbox) {
            $rendererClass .= '_Selectbox';
        } else if ($gridElement instanceof Zend_DataGrid_Column_Template) {
            $rendererClass .= '_Template';
        } else if ($gridElement instanceof Zend_DataGrid_Column) {
            $rendererClass .= '_Column';
        }

        $renderer = null;
        //verify if the class of renderer exist
        if (class_exists($rendererClass)) {
            $renderer = new $rendererClass($options);
        } else if (class_exists($basicRendererClass)) {
            $renderer = new $basicRendererClass($options);
        } else {
            $renderer = new Zend_DataGrid_Renderer($options);
        }
        $renderer->setGridElement($gridElement);

        return $renderer;
    }

    /**
     * Set the default renderer type
     *
     * @param string $name
     */
    public static function setDefaultType($rendererType)
    {
        if (empty($rendererType)) {
            require_once 'Zend/DataGrid/Renderer/Exception.php';
            throw new Zend_DataGrid_Renderer_Exception('The name of default renderer must not be empty');
        }
        self::$defaultRendererType = $rendererType;
    }

    /**
     * Gets the default renderer type
     *
     * @return string
     */
    public static function getDefaultType()
    {
        return self::$defaultRendererType;
    }

    /**
     * Get the type of render (HTml, Xml....)
     * @return string
     */
    public function getType()
    {
        return 'Null';
    }

    /**
     * Set the grid component (datagrid ou datagrid columns component) that should be rendered
     *
     * @param Zend_DataGrid|Zend_DataGrid_AssocColumns|Zend_DataGrid_Column $element
     * @return Zend_DataGrid_Renderer
     */
    public function setGridElement($element)
    {
        $this->_gridElement = $element;

        return $this;
    }

    /**
     * Gets the grid component that should be rendered
     *
     * @return null|Zend_DataGrid|Zend_DataGrid_AssocColumns|Zend_DataGrid_Column $element
     */
    public function getGridElement()
    {
        return $this->_gridElement;
    }


    //options


    /**
     * Initialize the renderer options. Use by extended classes to initialise the options
     *
     * @return Zend_DataGrid_Renderer
     */
    protected function initOptions() {}

    /**
     * Set renderer option
     *
     * @param  string $name
     * @param  mixed $value
     * @return Zend_DataGrid_Renderer
     */
    public function setOption($name, $value)
    {
        $this->_options[$name] = $value;

        return $this;
    }

    /**
     * Get the renderer option
     *
     * @param string $name
     * @return mixed
     */
    public function getOption($name)
    {
        return $this->_options[$name];
    }

    /**
     * Set renderer options
     *
     * @param array
     * @return Zend_DataGrid_Renderer
     */
    public function setOptions(array $options)
    {
        foreach ($options as $key => $val) {
            $this->setOption($key, $val);
        }

        return $this;
    }

    /**
     * Get renderer options
     *
     * @return array
     */
    public function getOptions()
    {
        return $this->_options;
    }


    //rendering


    /**
     * Set view object
     *
     * @param  Zend_View_Interface $view
     * @return Zend_DataGrid_Renderer
     */
    public function setView(Zend_View_Interface $view = null)
    {
        $this->_view = $view;

        return $this;
    }

    /**
     * Retrieve view object
     * Retrieves from ViewRenderer if none previously set.
     *
     * @return null|Zend_View_Interface
     */
    public function getView()
    {
        if (null === $this->_view) {
            require_once 'Zend/Controller/Action/HelperBroker.php';
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
            $this->setView($viewRenderer->view);
        }

        return $this->_view;
    }

    /**
     * Render the content
     *
     * @param Zend_View_Interface $view
     * @return string
     */
    public function render(Zend_View_Interface $view = null) {}

    /**
     * Renders the header
     *
     * @return string
     */
    public function renderHeader() {}

    /**
     * Renders the cell
     *
     * @param mixed $rowData
     * @return string
     */
    public function renderItem($rowData) {}

    /**
     * Renders the items
     *
     * @return string
     */
    public function renderItems() {}

    /**
     * Renders the footer
     *
     * @return string
     */
    public function renderFooter() {}

    /**
     * Renders the pager
     *
     * @param Zend_View_Interface $view
     * @return string
     */
    public function renderPager(Zend_View_Interface $view = null) {}

}
